package org.oos;

import static org.oos.ApiUrlProvider.APIConstants.ATTRIBUTES_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.BASE_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.CATEGORIES_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.CHECKIN_SITE_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.CHECKIN_USER_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.CONTACTS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.LISTS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.OFFERS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.PRO_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.SEARCH_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.SITE_DETAILS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.SITE_MEDIA_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.SITE_OFFERS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.SITE_REVIEWS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.SOCIAL_ACTIVITY_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.TAGS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.USERS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.USER_ACTIVITY_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.USER_OFFERS_SLUG;
import static org.oos.ApiUrlProvider.APIConstants.USER_SITES_SLUG;
import static org.oos.ApiUrlProvider.Schemas.HTTP;

import com.google.api.client.http.GenericUrl;

/**
 * 
 * This enumeration provides the means to get the relevant path. It also
 * provides with a way to reset the paths to an alternative.
 * 
 * @author pablo.gonzalez@11870.com
 * 
 */
public enum ApiUrlProvider {

	// Base and generic paths:
	BaseSchema, BaseSubHost, BaseHost, BaseDomain, ApiDomain, Base, ProductionSite,
	// info interactions
	Search, SiteReviews, SiteDetails, Categories, Attributes, Users, UserSites, SiteMedia, Contacts, UserActivity, SocialActivity, Offers, SiteOffers, UserOffers,
	// Logged interactions
	UserCheckin, SiteCheckin,
	// Category feeds
	UserLists, UserTags;

	static interface Schemas {
		static final String HTTP = "http";
		static final String HTTPS = "https";
	}

	/**
	 * 
	 * Slugs used to build the urls.
	 * 
	 * @author pablo.gonzalez@11870.com
	 * 
	 */
	static interface APIConstants {
		static final String BASE_SLUG = "/api/v2";
		static final String PRO_SLUG = "/pro";
		static final String SEARCH_SLUG = "/search";
		static final String SITE_REVIEWS_SLUG = "/site-reviews";
		static final String SITE_DETAILS_SLUG = "/site-details";
		static final String CATEGORIES_SLUG = "/categories";
		static final String ATTRIBUTES_SLUG = "/categories";
		static final String CHECKIN_USER_SLUG = "/checkins/user";
		static final String CHECKIN_SITE_SLUG = "/checkins/site";
		static final String USERS_SLUG = "/users";
		static final String SITE_MEDIA_SLUG = "/site-media";
		static final String USER_SITES_SLUG = "/sites";
		static final String CONTACTS_SLUG = "/contacts";
		static final String USER_ACTIVITY_SLUG = "/activity";
		static final String SOCIAL_ACTIVITY_SLUG = "/social-activity";
		static final String LISTS_SLUG = "/lists";
		static final String TAGS_SLUG = "/tags";
		static final String OFFERS_SLUG = "/offers";
		static final String USER_OFFERS_SLUG = "/offers/user";
		static final String SITE_OFFERS_SLUG = "/offers/site";
	}

	private String url;

	static {
		setBaseUrl(null, "api", null);
	}

	private static final String API_DOMAIN_TEMPLATE = "%s://%s";
	private static final String API_SUBDOMAIN_TEMPLATE = "%s://%s.%s";

	public static void build() {
		// ApiDomain.url = BaseSchema + BaseSubHost.url + BaseHost;
		if (BaseSubHost.url == null) {
			ApiDomain.url = String.format(API_DOMAIN_TEMPLATE, BaseSchema,
					BaseHost);
		} else {
			ApiDomain.url = String.format(API_SUBDOMAIN_TEMPLATE, BaseSchema,
					BaseSubHost, BaseHost);
		}
		Base.url = ApiDomain + BASE_SLUG;
		Search.url = Base + SEARCH_SLUG;
		SiteReviews.url = Base + SITE_REVIEWS_SLUG;
		SiteDetails.url = Base + SITE_DETAILS_SLUG;
		Categories.url = Base + CATEGORIES_SLUG;
		Attributes.url = Base + ATTRIBUTES_SLUG;
		UserCheckin.url = Base + CHECKIN_USER_SLUG;
		SiteCheckin.url = Base + CHECKIN_SITE_SLUG;
		Users.url = Base + USERS_SLUG;
		UserSites.url = Base + USER_SITES_SLUG;
		SiteMedia.url = Base + SITE_MEDIA_SLUG;
		Contacts.url = Base + CONTACTS_SLUG;
		UserActivity.url = Base + USER_ACTIVITY_SLUG;
		SocialActivity.url = Base + SOCIAL_ACTIVITY_SLUG;
		UserLists.url = Base + LISTS_SLUG;
		UserTags.url = Base + TAGS_SLUG;
		ProductionSite.url = ApiDomain + PRO_SLUG;
		Offers.url = Base + OFFERS_SLUG;
		SiteOffers.url = Base + SITE_OFFERS_SLUG;
		UserOffers.url = Base + USER_OFFERS_SLUG;
	}

	public static void setBaseUrl(String schema, String subHost, String host) {
		if (schema == null) {
			schema = HTTP;
		}
		if (host == null) {
			host = "11870.com";
			if (subHost == null) {
				subHost = "api";
			}
		}
		BaseSchema.url = schema;
		BaseSubHost.url = subHost;
		BaseHost.url = host;
		build();
	}

	@Override
	public String toString() {
		return url;
	}

	public GenericUrl getUrl(String... slugs) {
		StringBuilder sb = new StringBuilder(url);
		for (int i = 0; i < slugs.length; i++) {
			sb.append("/").append(slugs[i]);
		}
		return new GenericUrl(sb.toString());
	}

}
